package com.cqc.datetimepicker02;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CalendarView;
import android.widget.DatePicker;
import android.widget.TimePicker;
import android.widget.Toast;

import java.util.Calendar;

public class MainActivity extends AppCompatActivity {

    private Context context;
    private DatePicker datePicker;
    private TimePicker timePicker;
    private Button btn1;
    private Button btn2;
    private Button btn3;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this;

        initDatePicker();
        initTimePicker();
        initCalendarView();

        initDatePickerDialog();
        initTimePickerDialog();
    }


    private void initDatePicker() {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);

        datePicker = (DatePicker) findViewById(R.id.dataPicker);
        //获取变化的值，若设置为0，那么现实的是1900年1月
        datePicker.init(year, month, day, new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker datePicker, int year, int month, int day) {
                ToastUtil.showShortToast(context, "您选择的日期是：" + year + "年" + (month + 1) + "月" + day + "日");
            }
        });
    }

    private void initTimePicker() {
        timePicker = (TimePicker) findViewById(R.id.timePicker);
        timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
            @Override
            public void onTimeChanged(TimePicker timePicker, int hour, int minute) {
                ToastUtil.showShortToast(context, "您选择的时间是：" + hour + "时" + minute + "分");
            }
        });
    }

    private void initCalendarView() {
        btn3 = (Button) findViewById(R.id.btn3);
        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, CalendarViewActivity.class);
                startActivity(intent);
            }
        });
    }

    private void initTimePickerDialog() {
        btn1 = (Button) findViewById(R.id.btn1);
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DatePickerFragment fragment = new DatePickerFragment();
                fragment.show(getSupportFragmentManager(), "DatePickerFragment");
            }
        });
    }

    private void initDatePickerDialog() {
        btn2 = (Button) findViewById(R.id.btn2);
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                TimePickerFragment fragment = new TimePickerFragment();
                fragment.show(getSupportFragmentManager(), "TimePickerFragment");
            }
        });
    }


}
